<?php

ini_set('display_errors', 1);
ini_set('mysqli.reconnect', 0);
$baseDir = dirname(dirname(dirname(dirname(__FILE__))));
set_include_path(get_include_path().PATH_SEPARATOR.$baseDir.PATH_SEPARATOR.$baseDir.'/incubator');
ini_set('display_errors', 1);
ini_set('error_log', './error.log');

include '../../testingfunctions.php';
include 'Pone/Pone_Exception.php';
include 'Pone/Database/Pone_Database_ConnectionFactory.php';
include_once './dbconfig.php'; // $config is defined here
include_once './test_setup.php'; // refreshTestData() is defined here

try
{
    refreshTestData($config);
    // /home/web/windows.php.net/docroot/downloads/snaps/
    $conn = Pone_Database_ConnectionFactory::getConnection($config);
    $stmt = $conn->createStatement();

    $stmt->addBatch("INSERT INTO test2 VALUES (1007, 'pcdinh1007', 1)");
    // $stmt->addBatch("INSERT INTO test2 VALUES (1008-, 'pcdinh5', 1)");
    // $stmt->addBatch("SELECT * FROM X");
    $stmt->addBatch("INSERT INTO test2 VALUES (1009, 'pcdinh1009', 1)");
    $stmt->addBatch("INSERT INTO test2 VALUES (1010, 'pcdinh1010', 1)");
    $conn->beginTransaction();
    $updateCounts = $stmt->executeBatch();

    for ($i = 0; $i < count($updateCounts); $i++)
    {
        assertTrue(isset($updateCounts[$i]['affected_rows']) && 1 === $updateCounts[$i]['affected_rows'] && isset($updateCounts[$i]['last_insert_id']) &&
        0 === $updateCounts[$i]['last_insert_id'], 'Expected: SQL command number '.($i + 1).' is executed successfully. Affected row count is 1.
        Last insert ID must be 0');
        echo '<br />';
    }

    $newUsername = $stmt->fetchOne('SELECT username FROM test2 WHERE id = 1007');
    assertTrue($newUsername === 'pcdinh1007', 'Expected: The username which has just been just inserted is pcdinh1007 although transaction is not commited yet');
    echo '<br />';

    $conn->commit();

    $newUsername = $stmt->fetchOne('SELECT username FROM test2 WHERE id = 1007');
    assertTrue($newUsername === 'pcdinh1007', 'Expected: The username which has just been just inserted is pcdinh1007');
    echo '<br />';
    $newUsername = $stmt->fetchOne('SELECT username FROM test2 WHERE id = 1009');
    assertTrue($newUsername === 'pcdinh1009', 'Expected: The username which has just been just inserted is pcdinh1009');
    echo '<br />';
    $newUsername = $stmt->fetchOne('SELECT username FROM test2 WHERE id = 1010');
    assertTrue($newUsername === 'pcdinh1010', 'Expected: The username which has just been just inserted is pcdinh1010');
    $stmt->close();
}
catch (Pone_Exception_DatabaseBatchUpdate $be)
{
    echo '<br />';
    assertTrue(false, 'Unexpected: An exception has been thrown. '.$e->getMessage());

    // Not all of the statements were successfully executed
    $counts = $be->getUpdateCounts();

    for ($i = 0; $i < count($counts); $i++)
    {

    }

    echo 'Pone_Exception_DatabaseBatchUpdate: '.$be->getMessage()."<br />";
    echo 'Pone_Exception_DatabaseBatchUpdate: '.$be->getNativeMessage()."<br />";
    echo 'Pone_Exception_DatabaseAccess: '.implode('. ', $be->getTraceMessages());

    try
    {
        $conn->rollback();
    }
    catch (Pone_Exception_DatabaseAccess $e)
    {
        echo 'Pone_Exception_DatabaseBatchUpdate: '.$e->getMessage()."<br />";
        echo 'Pone_Exception_DatabaseBatchUpdate: '.$e->getNativeMessage()."<br />";
        echo 'Pone_Exception_DatabaseAccess: '.implode('. ', $e->getTraceMessages());
        echo '<br />';
        assertTrue(false, 'Unexpected: An exception has been thrown. '.$e->getMessage());
    }
}
catch (Pone_Exception_DatabaseAccess $e)
{
    echo 'Pone_Exception_DatabaseAccess: '.$e->getMessage()."<br />";
    echo 'Pone_Exception_DatabaseAccess: '.$e->getNativeMessage()."<br />";
    echo 'Pone_Exception_DatabaseAccess: '.implode('. ', $e->getTraceMessages());
    // handle SQL exception
    $conn->rollback();
    echo '<br />';
    assertTrue(false, 'Unexpected: An exception has been thrown. '.$e->getMessage());
}

try
{
    $conn->close();
}
catch (Pone_Exception_DatabaseAccess $e)
{
    echo '<br />';
    assertTrue(false, 'Unexpected: An exception has been thrown. '.$e->getMessage());
}
?>